const serviceURL = {
    //   redisRequestUrl: `${process.env.VUE_APP_BASE_API}/redis/getLast7DayData`
    //   redisLast7DayRequestUrl: `${process.env.VUE_APP_BASE_API}/redis/getLast7DayData`
    //   redisCurrentSpecLineRequestUrl: `${process.env.VUE_APP_BASE_API}/redis/getCurrentSpecLine`
    //   redisCurrentRequestUrl: `${process.env.VUE_APP_BASE_API}/redis/getCurrentData`
    // redisLast7DayRequestUrl: 'http://116.116.1.190:8085/redis/getLast7DayData',
    // redisCurrentSpecLineRequestUrl: 'http://116.116.1.190:8085/redis/getCurrentSpecLine',
    // redisCurrentRequestUrl: 'http://116.116.1.190:8085/redis/getCurrentData'
    redisLast7DayRequestUrl: 'http://127.0.0.1:3000/getLast7DayData',
    redisCurrentSpecLineRequestUrl: 'http://127.0.0.1:3000/getCurrentSpecLine',
    redisCurrentRequestUrl: 'http://127.0.0.1:3000/getCurrentData'

}

function throwIfMissing(name = throwIfMissing.name) {
    throw new Error(`${name}() Missing parameter`)
}
// function newGuid() {
//   return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
//     const r = Math.random() * 16 | 0
//     const v = c === 'x' ? r : (r & 0x3 | 0x8)
//     return v.toString(16)
//   })
// }
// const guid = newGuid()
async function fetchGet(url = throwIfMissing(fetchGet.name), data = '', actionId = '') {
    // console.log('getUrl', url)
        // url = `${url}?id=${actionId}&data=${encodeURIComponent(data)}`
    url = `${url}`
    try {
        const response = await fetch(url, {
                method: 'GET',
                mode: 'cors'
            })
            // console.log('getResponse', response)
        if (response.ok) {
            return response.json()
        } else {
            return Promise.reject({
                status: response.status,
                statusText: response.statusText,
                flag: false
            }).json()
        }
    } catch (e) {
        console.log(e)
        return Promise.reject({
            status: e.status,
            statusText: e.statusText,
            flag: false
        })
    }
}
// async function fetchPost(url = throwIfMissing(fetchGet.name), data = '') {
//   const body = new FormData()
//   body.append('id', data)
//   try {
//     const response = await fetch(url, {
//       method: 'POST',
//       mode: 'cors',
//       body
//     })
//     if (response.ok) {
//       return response.json()
//     } else {
//       return Promise.reject({
//         status: response.status,
//         statusText: response.statusText,
//         flag: false
//       })
//     }
//   } catch (e) {
//     console.log(e)
//     return Promise.reject({
//       status: e.status,
//       statusText: e.statusText,
//       flag: false
//     })
//   }
// }
async function getLCDALast7daysData() {
    return await getRedisLast7DayRequest('getLCDALast7daysData')
}
async function getHCDALast7daysData() {
    return await getRedisLast7DayRequest('getHCDALast7daysData')
}
async function getEmsMAULast7Data() {
    return await getRedisLast7DayRequest('getEmsMAULast7Data')
}
async function getLowChillerLast7Data() {
    return await getRedisLast7DayRequest('getLowChillerLast7Data')
}
async function getMediumChillerLast7Data() {
    return await getRedisLast7DayRequest('getMediumChillerLast7Data')
}
async function getLCDAConvertData() {
    return await getRedisLast7DayRequest('getLCDAConvertData')
}
async function getHCDAConvertData() {
    return await getRedisLast7DayRequest('getHCDAConvertData')
}
async function getLeftScreenPMaxMonthly() {
    return await getRedisLast7DayRequest('getLeftScreenPMaxMonthly')
}
async function getLeftScreenPMaxDaily() {
    return await getRedisLast7DayRequest('getLeftScreenPMaxDaily')
}
async function getMediumChillerRightData() {
    return await getRedisLast7DayRequest('getMediumChillerRightData')
}
async function getFirstScreenLastWeekC() {
    return await getRedisLast7DayRequest('getFirstScreenLastWeekC')
}
async function getFirstScreenMonthlyC() {
    return await getRedisLast7DayRequest('getFirstScreenMonthlyC')
}
async function getFirstScreenLastWeekWater() {
    return await getRedisLast7DayRequest('getFirstScreenLastWeekWater')
}
async function getFirstScreenLastWeekGas() {
    return await getRedisLast7DayRequest('getFirstScreenLastWeekGas')
}
async function getFirstScreenMonthlyWater() {
    return await getRedisLast7DayRequest('getFirstScreenMonthlyWater')
}
async function getFirstScreenMonthlyGas() {
    return await getRedisLast7DayRequest('getFirstScreenMonthlyGas')
}
async function getFirstScreenSecondThirdRowData() {
    return await getRedisLast7DayRequest('getFirstScreenSecondThirdRowData')
}
async function getFirstScreenLastWeekYield() {
    return await getRedisLast7DayRequest('getFirstScreenLastWeekYield')
}
async function getFirstScreenMonthlyYield() {
    return await getRedisLast7DayRequest('getFirstScreenMonthlyYield')
}
async function getLCDALine() {
    return await getRedisCurrentSpecLineRequest('lcda')
}
async function getHCDALine() {
    return await getRedisCurrentSpecLineRequest('hcda')
}
async function getLowChillerBestLine() {
    return await getRedisCurrentSpecLineRequest('lowChillWater')
}
async function getMediumChillerBestLine() {
    return await getRedisCurrentSpecLineRequest('mediumChillWater')
}
async function getLCDACurrentData() {
    // return await getRedisCurrentRequest('lcda')
    return await getRedisCurrentRequest()
}
async function getHCDACurrentData() {
    return await getRedisCurrentRequest('hcda')
}
async function getMediumChillerCurrentData() {
    return await getRedisCurrentRequest('mediumChillWater')
}
async function getLowChillerCurrentData() {
    return await getRedisCurrentRequest('lowChillWater')
}
async function getMAUCurrentData() {
    return await getRedisCurrentRequest('mau')
}
async function getRedisLast7DayRequest(data) {
    let result = await fetchByString(serviceURL.redisLast7DayRequestUrl, data)
    const resultData = JSON.parse(result.data)
    result = {...result, data: resultData }
    return result
}
async function getRedisCurrentSpecLineRequest(data) {
    let result = await fetchByString(serviceURL.redisCurrentSpecLineRequestUrl, data)
    const resultData = result.data.map(e => JSON.parse(e))
    result = {...result, data: resultData }
    console.log('result=======', result)
    return result
}

async function getRedisCurrentRequest(data) {
    return await fetchByString(serviceURL.redisCurrentRequestUrl, data)
}

async function fetchByString(url, data) {
    // return await fetchPost(url, data)
    return await fetchGet(url, data)
}
export default {
    getLCDALast7daysData,
    getHCDALast7daysData,
    getEmsMAULast7Data,
    getLowChillerLast7Data,
    getMediumChillerLast7Data,
    getLCDAConvertData,
    getHCDAConvertData,
    getMediumChillerRightData,
    getFirstScreenLastWeekC,
    getFirstScreenMonthlyC,
    getFirstScreenLastWeekWater,
    getFirstScreenLastWeekGas,
    getFirstScreenMonthlyWater,
    getFirstScreenMonthlyGas,
    getFirstScreenSecondThirdRowData,
    getFirstScreenLastWeekYield,
    getFirstScreenMonthlyYield,
    getLCDALine,
    getHCDALine,
    getLowChillerBestLine,
    getMediumChillerBestLine,
    getLCDACurrentData,
    getHCDACurrentData,
    getLowChillerCurrentData,
    getMediumChillerCurrentData,
    getLeftScreenPMaxMonthly,
    getLeftScreenPMaxDaily,
    getMAUCurrentData
}